package myaction;

import model.User;

import com.opensymphony.xwork2.ActionSupport;

/**
 * Use customized action method (add, delete). It enables one class to be able
 * to provide information for various user responses.
 * 
 * Action with parameters. Struts2 invokes the setter method, instead of writing
 * into the variable directly.
 * 
 * @author rex
 * 
 */
public class UserAction extends ActionSupport {

	private String name;
	private int age;
	private User myUser;
	private String myResult;

	public UserAction() {
		name = "undefined name";
		age = -1;
	}

	public String add() {
		System.out.println(name);
		System.out.println(age);
		return SUCCESS;
	}

	public String delete() {
		return SUCCESS;
	}

	public String addModel() {
		System.out.println(myUser.getAge());
		System.out.println(myUser.getName());
		return SUCCESS;
	}
	
	public String dynamicAdd() {
		if (name.equals("undefined name") || name.equals("")) {
			myResult = "/user_add_fail.jsp";
		}
		else {
			myResult = "/user_add_success.jsp";
		}
		return SUCCESS;
	}

	public String addVerify() {
		if (!name.equals("admin")) {
			this.addFieldError("name", "your name is nonsensical lol");
			return ERROR;
		}
		add();
		return SUCCESS;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public User getUser() {
		return myUser;
	}

	public void setUser(User myUser) {
		this.myUser = myUser;
	}

	public String getResult() {
		return myResult;
	}

	public void setResult(String result) {
		myResult = result;
	}
}
